#include "myDevice.h"
#include "stm32f1xx_hal.h"
#include "adc.h"
#include <stdio.h>
#include "myUart.h"
#include "myFlash.h"
#include "oled.h"
#include "myKey.h"
#include "myTask.h"
#include "nrf24l01.h"

/*****************************ADC*************************/
int16_t My_adcData[adc_max] = {0};
adcValue_type adcValue;
adcValue_type adcMax;//量程最大值
adcValue_type adcMin;//量程最小值
adcValue_type adcMid;//量程中值
adcValue_type adcTransfer;
uint8_t flag_calibrate_scale = 0;
u8 buf_handle[]={0xAA,0xAA,0x01,0x0D,0xDF,0x7F,0x7F,0x7F,0x7F,0x00,0x00,0xD7,0x51};

u8 MainWindow_Flag = 0; //菜单标志位，控制显示
u8 menu_flag = 0;		//菜单标志位

u8 ENABLE_KEY = 0;		//按键使能


u8 Communication = 0; //通信方式选择
signed char tmp_id[3] = {0};

signed char Car_ID[3] = {0};
void adc_init(void)
{
	HAL_ADCEx_Calibration_Start(&hadc1); // adc校准
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)My_adcData, adc_max);
	adcMin.value1=2048;
	adcMin.value2=2048;
	adcMin.value3=2048;
	adcMin.value4=2048;
	adcMax.value1=2048;
	adcMax.value2=2048;
	adcMax.value3=2048;
	adcMax.value4=2048;
}

/*
 *adc数据处理
 *每通道的数据进行10次获取，数据的每一组的第一个和最后一个不要，并且将剩下的进行取平均值
 *
 */
u8 num_byte;
u8 buf_adcValue[16];
u8 buf_adc[4]={0};
void ADC_dispose(void)
{
	adcValue.value1 = adcValue.value2 = adcValue.value3 = adcValue.value4 = 0;
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)My_adcData, adc_max); // 因为你选择的软件触发，所以每次采集都需要开启一次
	static uint8_t i;
	for (i = 1; i <= 8; i++)//// 遍历10次，进行滤波,第一个和最后一个舍弃
	{ 
		adcValue.value2 += My_adcData[0 + 4 * i];
		adcValue.value1 += My_adcData[1 + 4 * i];
		adcValue.value4 += My_adcData[2 + 4 * i];
		adcValue.value3 += My_adcData[3 + 4 * i];
	}
	adcValue.value1 = adcValue.value1 / 8;
	adcValue.value2 = adcValue.value2 / 8;
	adcValue.value3 = adcValue.value3 / 8;
	adcValue.value4 = adcValue.value4 / 8;

	if (flag_calibrate_scale)
	{
		uart_send(&huart2,0x8001,(uint8_t *)&adcValue,sizeof(adcValue));
	}
	else
	{
		adcTransfer.value1=adcValue.value1-flash_p.adcMid.value1;
		adcTransfer.value2=adcValue.value2-flash_p.adcMid.value2;
		adcTransfer.value3=adcValue.value3-flash_p.adcMid.value3;
		adcTransfer.value4=adcValue.value4-flash_p.adcMid.value4;
		if(adcTransfer.value1>=4095)adcTransfer.value1=4095;
		else if(adcTransfer.value1<=0)adcTransfer.value1=0;
		if(adcTransfer.value2>=4095)adcTransfer.value2=4095;
		else if(adcTransfer.value2<=0)adcTransfer.value2=0;
		if(adcTransfer.value3>=4095)adcTransfer.value3=4095;
		else if(adcTransfer.value3<=0)adcTransfer.value3=0;
		if(adcTransfer.value4>=4095)adcTransfer.value4=4095;
		else if(adcTransfer.value4<=0)adcTransfer.value4=0;
		buf_adc[0]=adcTransfer.value1>>4;
		buf_adc[1]=adcTransfer.value2>>4;
		buf_adc[2]=adcTransfer.value3>>4;
		buf_adc[3]=adcTransfer.value4>>4;
		buf_handle[5]=adcTransfer.value1>>4;
		buf_handle[6]=adcTransfer.value2>>4;
		buf_handle[7]=adcTransfer.value3>>4;
		buf_handle[8]=adcTransfer.value4>>4;
//		HAL_UART_Transmit(&huart1, buf_handle, 13, 0xff);
		if(ENABLE_KEY)
		{
			if(Communication==0)
			HAL_UART_Transmit(&huart3, buf_handle, 13, 0xff);
			
			//HAL_UART_Transmit(&huart2, buf_handle, 13, 0xff);
			if(Communication==6)
			{
				if(NRF24L01_TxPacket(buf_handle,13)==TX_OK)
				{
				}
				else
				{
				printf("NRF24L01无线模块数据发送失败\r\n");
			} 
			}
		}
		else
		{
			buf_handle[5]=buf_handle[6]=0x7F;
			
			if(Communication==0)
			HAL_UART_Transmit(&huart3, buf_handle, 13, 0xff);
			
			//HAL_UART_Transmit(&huart2, buf_handle, 13, 0xff);
			if(Communication==6)
			{
				if(NRF24L01_TxPacket(buf_handle,13)==TX_OK)
				{
				}
				else
				{
				printf("NRF24L01无线模块数据发送失败\r\n");
				} 
			}
		}
	}
//		OLED_ShowNum(0,0,buf_handle[5],3,16);//左摇杆左右
//		OLED_ShowNum(0,2,buf_handle[6],3,16);//左摇杆上下
//		OLED_ShowNum(64+16,0,buf_handle[7],3,16);//右摇杆左右
//		OLED_ShowNum(64+16,2,buf_handle[8],3,16);//右摇杆上下
		//0~265
		if(MainWindow_Flag)
		{
			OLED_Set_Pos(6,2);
			if(!ENABLE_KEY)
			{
				send(37);
				send(38);
				send(39);
			}
			else
			{
				if(buf_handle[6]>=255)
				{
					send(11);
					send(22);
					send(22);
				}
				else
				{
					send((buf_handle[6]*100/255)/10);
					//OLED_Set_Pos(6,2);
					send((buf_handle[6]*100/255)%10);
					send(10);
				}
				
				u8 j = 0;
				if(buf_handle[6]<=64)
					j = 5;
				else if(buf_handle[6]>64&&buf_handle[6]<=127)
					j = 4;
				else if(buf_handle[6]>127&&buf_handle[6]<=192)
					j = 3;
				else 
					j = 2;
				for(u8 i = 6;i>2;i--)
				{
					OLED_Set_Pos(9,i);
					OLED_WR_Byte(0x00,OLED_DATA);
					OLED_WR_Byte(0x00,OLED_DATA);
				}
				u8 k = (buf_handle[6]*100/255)/3;
				for(u8 i = 6;i>j;i--)							//进度条显示
				{
					OLED_Set_Pos(9,i);
					
					u8 data = 0x00;
					for(u8 i = 0;k>0&&i<8;k--,i++)
					{
						data |= 0x80>>i;
					}
					OLED_WR_Byte(data,OLED_DATA);
					OLED_WR_Byte(data,OLED_DATA);
				}
			}
			
//		OLED_Set_Pos(80,6);
//		send(j);
	}
		
}
void scaleCalibrate_begin(void)
{
	flag_calibrate_scale=1;
}
void scaleCalibrate_end(u8* buf)
{
	flag_calibrate_scale=0;
	memcpy((uint8_t *)&adcMid,buf,8);
	printf("%d,%d,%d,%d,",adcMid.value1,adcMid.value2,adcMid.value3,adcMid.value4);
	memcpy((uint8_t *)&flash_p.adcMid,(uint8_t *)&adcMid,8);
	flash_p.adcMid.value1=adcMid.value1-2048;
	flash_p.adcMid.value2=adcMid.value2-2048;
	flash_p.adcMid.value3=adcMid.value3-2048;
	flash_p.adcMid.value4=adcMid.value4-2048;
	fuc_save_flashParameter();
}

u8 show_menu = 0;
u8 last_menu = 0;

//菜单
Menu_table_t table[30] =
{
	//current up down enter return  value
	
	{0,0,0,1,0,menu_func0,0},//0
	//一级菜单
	{1,4,2,5,0,menu_func1,0},		//1			//通信方式
	{2,1,3,9,0,menu_func1,2},		//2			//车辆编号
	{3,2,4,3,0,menu_func1,4},		//3
	{4,3,1,4,0,menu_func1,6},		//4
	
	//二级菜单
	{5,8,6,11,1,menu_func2,0},		//5	
	{6,5,7,12,1,menu_func2,2},		//6
	{7,6,8,13,1,menu_func2,4},		//7
	{8,7,5,14,1,menu_func2,6},
	
	//三级菜单
	
	//后续添加的二级菜单
	//current up down enter return  point  value
	
	{9,9,9,10,2,menu_func6,0},					//车辆编号
	
	
	//车辆选择
	{10,10,10,9,2,Enter_Car,0},
	
	//通信方式选择
	{11,11,11,5,1,Enter_Cun,0},
	{12,12,12,6,1,Enter_Cun,2},
	{13,13,13,7,1,Enter_Cun,4},
	{14,14,14,8,1,Enter_Cun,6},
};



void menu(void)				
{
	if(menu_flag)			//注意，满足条件说明此时在菜单中，要屏蔽手柄发送数据
	{
		ENABLE_KEY = 0;
		if(buf_handle[7]>200)					//右摇杆左
		{
			show_menu = table[show_menu].Return;
			//clear_flag = 1;
		}
		else if(buf_handle[7]<50)					//右摇杆右
		{
			show_menu = table[show_menu].Enter;
		}
		else if(buf_handle[8]>200)
		{
			show_menu = table[show_menu].Up;
			if(show_menu==9)
			{
				if(car_flag == 1)
				{
					tmp_id[0]++;
					if(tmp_id[0]>9)
						tmp_id[0] = 0;
				}
				else if(car_flag == 2)
				{
					tmp_id[1]++;
					if(tmp_id[1]>9)
						tmp_id[1] = 0;
				}
				else if(car_flag == 3)
				{
					tmp_id[2]++;
					if(tmp_id[2]>9)
						tmp_id[2] = 0;
				}
			}
		}
		else if(buf_handle[8]<50)
		{
			show_menu = table[show_menu].Down;
			if(show_menu==9)
			{
				if(car_flag == 1)
				{
					tmp_id[0]--;
					if(tmp_id[0]<0)
						tmp_id[0] = 9;
				}
				else if(car_flag == 2)
				{
					tmp_id[1]--;
					if(tmp_id[1]<0)
						tmp_id[1] = 9;
				}
				else if(car_flag == 3)
				{
					tmp_id[2]--;
					if(tmp_id[2]<0)
						tmp_id[2] = 9;
				}
			}
		}
		
	}	
//		OLED_Set_Pos(64,5);
//		send(car_flag);
//		send(show_menu/10);
//		send(show_menu%10);
//		
//	send(last_menu/10);
//	send(last_menu%10);
		
		if((last_menu!=show_menu) /*&& handle_flag*/)
		{
			if(show_menu <10 || show_menu >14)
			OLED_Clear();
			last_menu = show_menu;
			car_flag = 0;
			table[show_menu].current_operation(table[show_menu].Value);
			MainWindow_Flag = 0;
		}
		if(!(show_menu && menu_flag))  //show_menu == 0 时，说明处于主界面  menu_flag == 0 时，退出菜单 
		{
				
			menu_flag = 0;							//显示主页面时，摇杆不能切换菜单
		
			show_menu = 0;							//复位索引	
			
			
			table[show_menu].current_operation(table[show_menu].Value);
			MainWindow_Flag = 1;				//MainWindow_Flag = 1 时，显示主页面
		}
		
		if(car_flag)
		{
			OLED_ShowCHinese(16+24+12,0,0,12);
			if(!id_flag && car_flag == 1)
			{
				send2(10,24,0);
			}
			else if(!id_flag && car_flag == 2)
			{
				send2(10,32,0);
			}
			else if(!id_flag && car_flag ==3)
			{
				send2(10,40,0);
			}
			else if(id_flag)
			{
				send2(tmp_id[0],24,0);
			
				send2(tmp_id[1],32,0);
			
				send2(tmp_id[2],40,0);
				
			}
		}
		
}


u8 sec = 0;
u8 min = 0;
u8 hour = 0;
void show_time(void)
{
	sec++;
	if(sec>=60)
	{
		sec = 0;
		min++;
		if(min>=60)
		{
			min = 0;
			hour++;
			if(hour>=24)
			{
				hour = 0;
			}
		}
	}
}


//通信方式确认
void Enter_Cun(u8 value)
{
	Communication = value;
	
	show_menu = table[show_menu].Return;
	//OLED_ShowCHinese(60,value,33,12);
}


//确认选择车辆
void Enter_Car(u8 value)
{
	uint16 tmp = 0;
	
	memcpy(Car_ID,tmp_id,sizeof(tmp_id));
	
	tmp = tmp_id[0]*100+tmp_id[1]*10+tmp_id[2];
	
	
	RX_ADDRESS[3] = (tmp >> 8) & 0xFF;
	RX_ADDRESS[4] = tmp & 0xFF;
	
	TX_ADDRESS[3] = (tmp >> 8) & 0xFF;
	TX_ADDRESS[4] = tmp & 0xFF;
	
	NRF24L01_TX_Mode();
	
	show_menu = 9;
	car_flag = 0;
	
	
}


void menu_func1(u8 value)
{
	
	
	OLED_ShowCHinese(0,value,1,12);
	//OLED_ShowString(0,0,(u8 *)"->",16);
	OLED_ShowCHinese(16,2,30,12); 	//车
	OLED_ShowCHinese(16+12,2,31,12); 	//辆
	OLED_ShowCHinese(16+24,2,32,12); 	//编
	OLED_ShowCHinese(16+36,2,6,12); 	//号
	
	send2(Car_ID[0],100,2);
	send2(Car_ID[1],108,2);
	send2(Car_ID[2],116,2);
	
	 
	OLED_ShowCHinese(16,0,36,12);			//通
	OLED_ShowCHinese(16+12,0,37,12);	//信
	OLED_ShowCHinese(16+24,0,38,12);	//方
	OLED_ShowCHinese(16+36,0,39,12);	//式
	
	
	OLED_ShowCHinese(16,4,7,12);
	OLED_ShowCHinese(16+12,4,8,12);
	OLED_ShowCHinese(16+24,4,18,12);
	OLED_ShowCHinese(16,6,7,12);
	OLED_ShowCHinese(16+12,6,8,12);
	OLED_ShowCHinese(16+24,6,19,12);
}


void menu_func2(u8 value)
{
	OLED_ShowCHinese(0,value,1,12);
	//OLED_ShowString(0,0,(u8 *)"->",16);
	OLED_ShowString(16,0,(u8*)"485",16);
	OLED_ShowString(16,2,(u8*)"CAN",16);
	OLED_ShowCHinese(16,4,40,12);			//以
	OLED_ShowCHinese(16+12,4,41,12);	//太
	OLED_ShowCHinese(16+24,4,42,12);	//网
	
	OLED_ShowString(16,6,(u8*)"2.4G",16);
	
	OLED_ShowCHinese(60,Communication,33,12);
	
}                 





void menu_func0(u8 value)
{
	if(MainWindow_Flag == 0)
	{
		OLED_DisplayMain();
		uint16 tmp = RX_ADDRESS[3]*256+RX_ADDRESS[4];
		Car_ID[0] = tmp/100;
		Car_ID[1] = (tmp/10)%10;
		Car_ID[2] = tmp %10;
		
		OLED_ShowCHinese(64+6,3,34,12);						//设
		OLED_ShowCHinese(64+12+6,3,35,12);				//备
		OLED_ShowCHinese(64+24+6,3,32,12);				//编
		OLED_ShowCHinese(64+36+6,3,6,12);					//号
		
		send2(Car_ID[0],92,5);
		send2(Car_ID[1],100,5);
		send2(Car_ID[2],108,5);
	}
	send2(sec/10,64+28+6+8,0);
	send2(min/10,64+14+7,0);
	send2(hour/10,64,0);

	send2(hour%10,64+8,0);
	send2(min%10,64+22+7,0);
	send2(sec%10,64+36+8+6,0);
}

void menu_func6(u8 value)
{

	OLED_ShowCHinese(0,value,1,12);
	
	send2(Car_ID[0],16+8,0);
	send2(Car_ID[1],16+16,0);
	send2(Car_ID[2],16+24,0);
	
	memcpy(tmp_id,Car_ID,sizeof(Car_ID));
	if(!car_flag)
	{
		OLED_ShowCHinese(16+24+12,0,33,12);
	}
	
}





